package com.stylefeng.guns.modular.flowers.controller;

import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.FlowerMapper;
import com.stylefeng.guns.common.persistence.model.FlowerType;
import com.stylefeng.guns.common.persistence.model.Flowers;
import com.stylefeng.guns.common.redis.RedisUtils;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.flowerType.service.IFlowerTypeService;
import com.stylefeng.guns.modular.flowers.service.IFlowersService;
import com.stylefeng.guns.modular.system.warpper.FlowersWarpper;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * flowers控制器
 *
 * @author fengshuonan
 * @Date 2019-07-11 15:57:15
 */

@Controller
@RequestMapping("/flowers")
public class FlowersController extends BaseController {

    private String PREFIX = "/flowers/flowers/";

    @Resource
    private IFlowersService flowersService;

    @Resource
    private FlowerMapper flowerMapper;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private IFlowerTypeService flowerTypeService;

    /**
     * 跳转到flowers首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "flowers.html";
    }

    /**
     * 跳转到添加flowers
     */
    @RequestMapping("/flowers_add")
    public String flowersAdd(Model model) {
        List<Map<String, Object>> list = flowerTypeService.showFlowerTypeTable(null);
        model.addAttribute("list",list);
        return PREFIX + "flowers_add.html";
    }

    /**
     * 跳转到修改flowers
     */
    @RequestMapping("/flowers_update/{flowersId}")
    public String flowersUpdate(@PathVariable Integer flowersId, Model model) {
        Flowers flowers = flowerMapper.selectById(flowersId);
        FlowerType flowerTypes = flowersService.selFlowerType(flowers.getFid());
        List<Map<String, Object>> list = flowerTypeService.showFlowerTypeTable(null);
        model.addAttribute(flowers);
        model.addAttribute("flowerTypes", flowerTypes);
        model.addAttribute("list",list);
        return PREFIX + "flowers_edit.html";
    }

    /**
     * 获取flowers列表
     */
    @ApiOperation("查看花卉列表")
    @PostMapping(value = "/list")
    @ResponseBody
    public Object list(@ModelAttribute String condition) {
        List<Map<String, Object>> flower = flowersService.showFlowersTable(condition);
        return super.warpObject(new FlowersWarpper(flower));
    }

    /**
     * 新增flowers
     */
    @ApiOperation("添加花卉花木")
    @PostMapping(value = "/add")
    @ResponseBody
    public Object addFlower(@ModelAttribute Flowers flowers) {
        redisUtils.set("flowers", flowers);
        flowersService.addFlower(flowers);

        return super.SUCCESS_TIP;
    }

    /**
     * 删除flowers
     */
    @ApiOperation("删除花卉信息")
    @PostMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer flowersId) {
        flowerMapper.deleteById(flowersId);
        return SUCCESS_TIP;
    }


    /**
     * 修改flowers
     */
    @ApiOperation("修改花卉信息")
    @PostMapping(value = "/update")
    @ResponseBody
    public Object update(@ModelAttribute Flowers flowers) {
        if (ToolUtil.isEmpty(flowers) || flowers.getId() == null) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        flowerMapper.updateById(flowers);
        return super.SUCCESS_TIP;
    }

    /**
     * flowers详情
     */
    @ApiOperation("查看花卉详情")
    @PostMapping(value = "/detail/{flowersId}")
    public String detail(@PathVariable Integer flowersId, Model model) {
        if (ToolUtil.isEmpty(flowersId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        Flowers flowers = flowerMapper.selectById(flowersId);
        FlowerType flowerType = flowersService.selFlowerType(flowers.getFid());
        model.addAttribute(flowers);
        model.addAttribute(flowerType);
        return PREFIX + "flowers_detail.html";
    }
}